//
//  LTFReadDetailView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/23.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFReadDetailView.h"
#import "LTFReadDetailModel.h"
#import "LTFReadDetailTableCell.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import <WebKit/WebKit.h>
@interface LTFReadDetailView ()
<
 UITableViewDataSource,
 UITableViewDelegate,
 WKUIDelegate,
 WKNavigationDelegate
>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tableViewArray;
@property (nonatomic, retain) WKWebView *webView;
@end

@implementation LTFReadDetailView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string copy];
        [self createData];
    }
}

- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
    [self addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 150;
    [_tableView registerClass:[LTFReadDetailTableCell class] forCellReuseIdentifier:NSStringFromClass([LTFReadDetailTableCell class])];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableViewArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFReadDetailTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFReadDetailTableCell class]) forIndexPath:indexPath];
    LTFReadDetailModel *model = self.tableViewArray[indexPath.row];
    [cell.titleImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    cell.title.text = model.title;
    cell.date.text = [model.ptime substringToIndex:10];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFReadDetailModel *model = self.tableViewArray[indexPath.row];
    [self.delegate sendString:model.postid];

}

//解析地址
- (void)createData {
    self.tableViewArray = [NSMutableArray array];
    
    NSString *string = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/%@/0-20.html", self.string];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        NSArray *array = [dictionary objectForKey:[NSString stringWithFormat:@"%@", self.string]];
        self.tableViewArray = [LTFReadDetailModel BaseModelByArray:array];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}
@end
